﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using Jw.Core;
using Newtonsoft.Json.Linq;

using System.ComponentModel.DataAnnotations;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class WxjlController : Controller
    {
        //
        // GET: /Dev/Wxjl/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {

                idField = "Wxjl_BillNo", //主键
                urls = DevHelper.GetIndexUrls("wxjl"),
                resx = DevHelper.GetIndexResx("维修记录"),
                dataSource = new
                {

                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()

                },
                form = new
                {
                    Wxjl_BillNo = "",
                    Wxjl_Sbbh = "",
                    Wxjl_Kssj = "",
                    Wxjl_Jssj = "",
                    Wxjl_Remark=""
                }

            };
            return View(model);
        }

        public ActionResult Edit(string id = "")
        {
            var data = new WxjlApiController().GetEditMaster(id);
            var codeService = new sys_codeService();
            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = DevHelper.GetEditUrls("wxjl"),
                resx = DevHelper.GetEditResx("维修记录"),
                dataSource = new
                {
                },
                defaultForm = new b_Wxjl().Extend(new
                {
                    Wxjl_BillNo = id,
                    Wxjl_Kssj = DateTime.Now,
                    Wxjl_Jssj = DateTime.Now,
                    Wxjl_Remark = ""
                }),
                defaultRow = new
                {
                    Wxjl_Wxgs = 0.00,
                    Wxjl_Tjgs = 0.00,
                    Wxjl_Wxfy = 0.00,
                    Wxjl_Wxrs=0

                },
                setting = new
                {
                    postFormKeys = new string[] { "Wxjl_BillNo" },
                    //更新的明细字段
                    postListFields = new string[] { "Wxjlmx_BillNo", "Wxjlmx_Flbh", "Wxjlmx_Wxxm", "Wxjlmx_Wxyq", "Wxjlmx_Fzry", 
                    "Wxjlmx_Jcry","Wxjlmx_Remark" }
                }
            };

            return View(model);
        }

    }

    public class WxjlApiController : ApiController
    {
        public string GetNewBillNo()
        {
            return new b_WxjlService().GetNewKey("Wxjl_BillNo", "dateplus");
        }
        public string GetNewRowId(int id, string BillNo)
        {
            return new b_WxjlmxService().GetNewKey("Wxjlmx_BillNo", "maxplus", id, ParamQuery.Instance().AndWhere("Wxjlmx_BillNo", BillNo));
        }

        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Wxjl_BillNo DESC'>
            <select>A.*,B.Sbda_Sbmc,B.Sbda_Ggxh</select>
                <from>
                    b_Wxjl A
                    Left Join b_Sbda B on B.Sbda_Sbbm = A.Wxjl_Sbbh
                </from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Wxjl_Sbbh'     cp='like'></field>
                        <field name='Wxjl_Kssj' cp='daterange'></field>
                        <field name='Wxjl_Jssj' cp='daterange'></field>
                    </where>
            </settings>");
            var service = new b_WxjlService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);
            return result;
        }
        public dynamic GetEditMaster(string id)
        {
            b_Wxjl obj = new b_WxjlService().GetModel(ParamQuery.Instance().AndWhere("Wxjl_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new b_WxjlService().ScrollKeys("Wxjl_BillNo", id.ToString())
            };
        }

        // 地址：GET api/qms/Ldphbz/getdetail
        public dynamic GetDetail(string id)
        {
            var query = RequestWrapper
               .InstanceFromRequest()
               .SetRequestData("Wxjlmx_BillNo", id)
               .LoadSettingXmlString(@"
            <settings defaultOrderBy='Wxjlmx_Flbh'>
                <select>
                    *
                </select>
                <from>
                    b_Wxjlmx
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true'>
                    <field name='Wxjlmx_BillNo' cp='equal'></field>
                </where>
            </settings>");

            var pQuery = query.ToParamQuery();
            var result = new b_WxjlmxService().GetDynamicListWithPaging(pQuery);
            return result;

        }

        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Wxjl_BillNo' cp='equal'></field></where>
            </settings>", typeof(b_Wxjl).Name);

            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where>
                    <field name='Wxjlmx_BillNo' cp='equal'></field>
                    <field name='Wxjlmx_Flbh'  cp='equal'></field>
                </where>
            </settings>", typeof(b_Wxjlmx).Name);

            var result = new b_WxjlService().Edit(formWrapper, listWrapper, data);
        }

        /// <summary>
        /// 删除主表（删除主表前是删除明细)
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new b_WxjlService().Delete(ParamDelete.Instance().AndWhere("Wxjl_BillNo", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Wxjl_BillNo={0}]，请重试或联系管理员！", id);
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Wxjl).Name, "Wxjl_BillNo", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Wxjl_BillNo={0}]，请重试或联系管理员！", id);
        }
    }
}
